/**
 * Copyright (c) Software Institute of PKU
 * All rights reserved. 
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */
package edu.pku.sei.transformation.editor.controller.editparts.node;

import java.beans.PropertyChangeEvent;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.MouseEvent;
import org.eclipse.draw2d.MouseMotionListener;
import org.eclipse.draw2d.PolylineConnection;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.ConnectionEditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.NodeEditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.editparts.AbstractConnectionEditPart;

import edu.pku.sei.modeler.model.structure.MetaNode;
import edu.pku.sei.modeler.model.structure.MetaShapeElement;
import edu.pku.sei.transformation.editor.controller.policies.CommonXYLayoutPolicy;
import edu.pku.sei.transformation.editor.model.GraphicalDomain;
import edu.pku.sei.transformation.editor.model.ModelPackage;
import edu.pku.sei.transformation.editor.views.figures.DomainFigure;
import edu.pku.sei.transformation.editor.views.figures.GraphicalDomainFigure;

/**
 * @generated
 * 
 */
public class GraphicalDomainEditpart extends DomainEditpart {
	public GraphicalDomainEditpart(MetaShapeElement model) {
		super(model);
	}

	protected IFigure createFigure() {
		DomainFigure figure = new GraphicalDomainFigure();
		figure.addMouseMotionListener(new MouseMotionListener() {

			@Override
			public void mouseMoved(MouseEvent me) {
			}

			@Override
			public void mouseHover(MouseEvent me) {
			}

			@Override
			public void mouseExited(MouseEvent me) {
				if (!getGraphicalDomain().isCollapsed()
						&& getChildren().size() > 0) {
					((GraphicalDomainFigure) getFigure()).hide();
					((PolylineConnection) ((AbstractConnectionEditPart) getTargetConnections()
							.get(0)).getFigure())
							.setTargetAnchor(((NodeEditPart) getChildren().get(
									0))
									.getTargetConnectionAnchor((ConnectionEditPart) getTargetConnections()
											.get(0)));
				}
			}

			@Override
			public void mouseEntered(MouseEvent me) {
				((GraphicalDomainFigure) getFigure()).show();
				((PolylineConnection) ((AbstractConnectionEditPart) getTargetConnections()
						.get(0)).getFigure())
						.setTargetAnchor(getTargetConnectionAnchor((ConnectionEditPart) getTargetConnections()
								.get(0)));
			}

			@Override
			public void mouseDragged(MouseEvent me) {
				// TODO Auto-generated method stub

			}
		});
		// figure.add(header);
		return figure;
	}

	@Override
	protected void createEditPolicies() {
		super.createEditPolicies();
		installEditPolicy(EditPolicy.LAYOUT_ROLE, new CommonXYLayoutPolicy());
	}

	@Override
	public void performRequest(Request request) {
		if (request.getType() == RequestConstants.REQ_OPEN) {
			this.getGraphicalDomain().setCollapsed(
					!this.getGraphicalDomain().isCollapsed());
		} else {
			super.performRequest(request);
		}

	}

	@Override
	public IFigure getContentPane() {
		return ((GraphicalDomainFigure) getFigure()).getBody();
	}

	private GraphicalDomain getGraphicalDomain() {
		return (GraphicalDomain) ((MetaNode) this.getModel()).getModelElement();
	}

	@Override
	protected void refreshVisuals() {
		Rectangle constraint = new Rectangle(0, 0, -1, -1);
		MetaNode node = getNode();
		GraphicalDomain domain = this.getGraphicalDomain();
		GraphicalDomainFigure figure = (GraphicalDomainFigure) this.getFigure();

		Point p = new Point(node.getLocationX(), node.getLocationY());
		constraint.setLocation(p);
		if (domain.isCollapsed()) {
			constraint.width = 160;
			constraint.height = 100;
		} else {
			constraint.width = node.getWidth();
			constraint.height = node.getHeight();
		}
		((GraphicalEditPart) getParent()).setLayoutConstraint(this,
				getFigure(), constraint);

		if (domain.isCollapsed()) {
			try {
				figure.remove(((GraphicalDomainFigure) getFigure()).getBody());
			} catch (Exception e) {
			}
			figure.fillFigure();
		} else {
			figure.add(getContentPane());
			figure.unfillFigure();

		}
	}

	@Override
	protected void handlePropertyChanged(PropertyChangeEvent event) {
		String propertyName = event.getPropertyName();
		if (ModelPackage.GRAPHICAL_DOMAIN_COLLAPSE.equals(propertyName)) {
			refreshVisuals();
		} else {
			super.handlePropertyChanged(event);
		}
	}

}
